package dao.mysql;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import util.DataBaseConnection;

import bean.Guia_de_entrada;
import dao.Entidad;

public class MySQLGuia_de_entradaDAO implements Entidad<Guia_de_entrada> {

	@Override
	public Guia_de_entrada insert(Guia_de_entrada t) {
		Connection con=null;
		try {
			con=DataBaseConnection.openConnection();
			String sql="INSERT INTO guia_de_entrada(id_guia_entrada,fecha,hora,descripcion) VALUES(?,?,?,?)";
			PreparedStatement ps=con.prepareStatement(sql,PreparedStatement.RETURN_GENERATED_KEYS);
			ps.setInt(1, t.getId_guia_entrada());
			ps.setObject(2, t.getFecha());
			ps.setObject(3, t.getHora());
			ps.setString(4, t.getDescripcion());
			ps.executeUpdate();
			ResultSet rs=ps.getGeneratedKeys();
			if(rs.next()){
				int id=rs.getInt(1);
				t.setId_guia_entrada(id);
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DataBaseConnection.closeConnection(con);
		}
		return t;
	}

	@Override
	public int update(Guia_de_entrada t) {
		Connection con=null;
		int rows = -1;
		try {
			con=DataBaseConnection.openConnection();
			String sql="UPDATE guia_de_entrada SET fecha=?,hora=?,descripcion=? ";
			sql = sql +	"WHERE id_guia_entrada=?";
			PreparedStatement ps=con.prepareStatement(sql);
			ps.setObject(1, t.getFecha());
			ps.setObject(2, t.getHora());
			ps.setString(3, t.getDescripcion());
			rows =ps.executeUpdate();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DataBaseConnection.closeConnection(con);
		}
		return rows;
	}

	@Override
	public int delete(Guia_de_entrada t) {
		Connection con=null;
		int rows=-1;
		try {
			con=DataBaseConnection.openConnection();
			String sql="DELETE FROM guia_de_entrada WHERE id_guia_entrada=?";
			PreparedStatement ps=con.prepareStatement(sql);
			ps.setInt(1, t.getId_guia_entrada());
			rows=ps.executeUpdate();
			System.out.println(rows);
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DataBaseConnection.closeConnection(con);
		}
		return rows;
	}

	@Override
	public Guia_de_entrada findById(Guia_de_entrada t) {
		// TODO Auto-generated method stub
				Connection con=null;
				try {
					con=DataBaseConnection.openConnection();
					String sql="SELECT * FROM guia_de_entrada WHERE id_guia_entrada=?";
					PreparedStatement ps=con.prepareStatement(sql);
					ps.setInt(1, t.getId_guia_entrada());
					ResultSet rs=ps.executeQuery();
					while(rs.next()){
						t.setId_guia_entrada(rs.getInt("id_guia_entrada"));
						t.setFecha(rs.getDate("fecha"));
						t.setHora(rs.getDate("hora"));
						t.setDescripcion(rs.getString("descripcion"));	
					}
				}catch(Exception e){
					e.printStackTrace();
				}finally{
					DataBaseConnection.closeConnection(con);
				}
				return t;
	}

	@Override
	public List<Guia_de_entrada> listAll() {
		// TODO Auto-generated method stub
				List<Guia_de_entrada> guias = new ArrayList<Guia_de_entrada>();
				Connection con=null;
				try {
					con=DataBaseConnection.openConnection();
					String sql="SELECT * FROM guia_de_entrada";
					Statement st=con.createStatement();
					ResultSet rs=st.executeQuery(sql);
					while(rs.next()){
						Guia_de_entrada t = new Guia_de_entrada();
						t.setId_guia_entrada(rs.getInt("id_guia_entrada"));
						t.setFecha(rs.getDate("fecha"));
						t.setHora(rs.getDate("hora"));
						t.setDescripcion(rs.getString("descripcion"));
						guias.add(t);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}finally{
					DataBaseConnection.closeConnection(con);
				}
				return guias;
	}

	@Override
	public List<Guia_de_entrada> listAllCustom(Object... objects) {
		// TODO Auto-generated method stub
		return null;
	}

	

}
